package com.witsight.platform.config;

import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;

/**   
 * 说明：[]
 * @Title: CustomMessageSourceAutoConfiguration.java 
 * @Package com.witsight.platform.config 
 * @See: []<br/>
 * Copyright: Copyright (c) 2017<br/>
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年11月23日 下午2:50:19 
 * @version: V1.0
 *
 */
@Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.messages")
@ConditionalOnClass({ MessageSourceAutoConfiguration.class})
public class CustomMessageSourceAutoConfiguration extends MessageSourceAutoConfiguration {
	@Override
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		if (StringUtils.hasText(this.getBasename())) {
			messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
					StringUtils.trimAllWhitespace(this.getBasename())));
		}
		if (this.getEncoding() != null) {
			messageSource.setDefaultEncoding(this.getEncoding().name());
		}
		messageSource.setFallbackToSystemLocale(this.isFallbackToSystemLocale());
		messageSource.setCacheSeconds(this.getCacheSeconds());
		messageSource.setAlwaysUseMessageFormat(this.isAlwaysUseMessageFormat());
		return super.messageSource();
	}
}
